package model.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import dom.domain.Generaltype;
import dom.domain.GeneraltypeDAO;
import dom.domain.Subtype;
import dom.domain.SubtypeDAO;
import dom.domain.SubtypeId;

public class SubTypeService {
	private SubtypeDAO subDao;
	private GeneraltypeDAO gDao;
	private Map<String,String> map;

	
	
	public void setgDao(GeneraltypeDAO gDao) {
		this.gDao = gDao;
	}

	public void setSubDao(SubtypeDAO subDao) {
		this.subDao = subDao;
	}
	public String delete(String Info){
		String [] subtypeIds=  Info.split(",");
		StringBuilder result = new StringBuilder();
		for(int i = 0;i<subtypeIds.length;i++)
		{
			String[] temp = subtypeIds[i].split("#");
			SubtypeId subId = new SubtypeId(Integer.parseInt(temp[0]),Integer.parseInt(temp[1]));
			JSONObject jInfo = JSONObject.fromObject(delete(subId));
			if(jInfo.getString("flag").equals("false"))
			{
				result.append(jInfo.getString("Info"));
			}
		}
		map.put("flag","true");
		map.put("Info",result.toString());
		return JSONObject.fromObject(map).toString();
	}
	public String add(String Info)
	{
		map = new HashMap<String,String>();
		try
		{
			JSONObject jInfo = JSONObject.fromObject(Info);
			if(!jInfo.containsKey("gid"))
			{
				map.put("flag", "false");
				map.put("Info", "gid为空");
				return JSONObject.fromObject(map).toString();
			}
			if(!(jInfo.containsKey("name")&&!jInfo.getString("name").equals("")))
			{
				map.put("flag", "false");
				map.put("Info", "name为空");
				return JSONObject.fromObject(map).toString();
			
			}
			Generaltype gType = gDao.findById(Integer.parseInt(jInfo.getString("gid")));
			if(gType==null){
				map.put("flag", "false");
				map.put("Info", "sid不存在");
				return JSONObject.fromObject(map).toString();
			}
			Subtype subType = new Subtype();
			SubtypeId subTypeId = new SubtypeId();
			subTypeId.setGid(Integer.parseInt(jInfo.getString("gid")));
			int count = subDao.getCount(Integer.parseInt(jInfo.getString("gid")));
			subTypeId.setSid(count+1);
			subType.setId(subTypeId);
			subType.setName(jInfo.getString("name"));
			subType.setExpertNo(3);
			subDao.save(subType);
			map.put("flag","true");
			map.put("Info","添加成功");
		}catch(JSONException e)
		{
			map.put("flag", "false");
			map.put("Info", e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String delete(SubtypeId id)
	{
		map = new HashMap<String,String>();
		if(id!=null)
		{
			Subtype sub = subDao.findById(id);
			if(sub!=null)
			{
				subDao.delete(sub);
				subDao.update(id.getSid(), id.getGid());
				map.put("flag", "true");
				map.put("Info", "删除成功");
			}
			else
			{
				map.put("flag", "false");
				map.put("Info","id不存在");
			}
		}
		else
		{
			map.put("flag", "false");
			map.put("Info", "id为空");
		}
		return JSONObject.fromObject(map).toString();
	}
	public String modify(String Info)
	{
		map = new HashMap<String,String>();
		try
		{
			JSONObject jInfo = JSONObject.fromObject(Info);
			if(!jInfo.containsKey("gid"))
			{
				map.put("flag", "false");
				map.put("Info", "gid为空");
				return JSONObject.fromObject(map).toString();
			}
			if(!jInfo.containsKey("sid"))
			{
				map.put("flag", "false");
				map.put("Info", "sid为空");
				return JSONObject.fromObject(map).toString();
			
			}
			if(!jInfo.containsKey("name"))
			{
				map.put("flag", "false");
				map.put("Info", "name为空");
				return JSONObject.fromObject(map).toString();
			}
			Generaltype gType = gDao.findById(Integer.parseInt(jInfo.getString("sid")));
			if(gType==null){
				map.put("flag", "false");
				map.put("Info", "sid不存在");
				return JSONObject.fromObject(map).toString();
			}
			
			SubtypeId subTypeId = new SubtypeId();
			subTypeId.setSid(Integer.parseInt(jInfo.getString("sid")));
			subTypeId.setGid(Integer.parseInt(jInfo.getString("gid")));
			Subtype subType = subDao.findById(subTypeId);
			if(subType==null)
			{
				map.put("flag", "false");
				map.put("Info", "id不存在");
				return JSONObject.fromObject(map).toString();
			}
			subType.setName(jInfo.getString("name"));
			subDao.attachDirty(subType);
			map.put("flag","true");
			map.put("Info","修改成功");
		}catch(JSONException e)
		{
			map.put("flag", "false");
			map.put("Info", e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String getAll( String gid )
	{
		map = new HashMap<String,String>();
		if(gid==null)
		{
			map.put("flag", "false");
			map.put("Info", "gid为空");
			return JSONObject.fromObject(map).toString();
		}
		JSONArray array  = new JSONArray();
		List list = subDao.findByGid(gid);
		for(int i=0;i<list.size();i++)
		{
			Subtype subType = (Subtype) list.get(i);
			array.add(JSONObject.fromObject(subType.toString()));
		}
		if(array.size()==0)
		{
			map.put("flag", "false");
			map.put("Info", "未添加任何数据");
		}else
		{
			map.put("flag", "true");
			map.put("Info", "获取成功");
			map.put("array", array.toString());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String getAll()
	{
		map = new HashMap<String,String>();
		JSONArray array = new JSONArray();
		List list = subDao.findAll();
		for(int i = 0; i < list.size(); i++)
		{
			Subtype subType = (Subtype) list.get(i);
			array.add(JSONObject.fromObject(subType.toString()));
		}
		if(array.size()==0){
			map.put("flag", "false");
			map.put("Info", "未添加任何数据");
		}
		else
		{
			map.put("flag", "true");
			map.put("Info", "获取成功");
			map.put("array", array.toString());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String getAll(int pageNum)
	{
		map = new HashMap<String,String>();
		JSONArray array = new JSONArray();
		List list = subDao.getList(pageNum, 10);
		for(int i = 0; i < list.size(); i++)
		{
			Subtype subType = (Subtype) list.get(i);
			Generaltype gType = gDao.findById(subType.getId().getGid());
			JSONObject jInfo = JSONObject.fromObject(subType.toString());
			jInfo.put("gname", gType.getName());
			array.add(jInfo);
			
		}
		if(array.size()==0){
			map.put("flag", "false");
			map.put("Info", "未添加任何数据");
		}
		else
		{
			map.put("flag", "true");
			map.put("Info", "获取成功");
			map.put("array", array.toString());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String query(String name)
	{
		if(name == null){
			map.put("flag", "false");
			map.put("Info", "name为空");
			return JSONObject.fromObject(map).toString();
		}
		List general = gDao.query(name);
		JSONArray array = new JSONArray();
		for(int i = 0;i<general.size();i++)
		{
			Generaltype gType = (Generaltype)general.get(i);
			List subList = subDao.findByGid(gType.getId());
			for(int j=0;j<subList.size();j++)
			{
				Subtype subType = (Subtype) subList.get(i);
				JSONObject jInfo = JSONObject.fromObject(subType.toString());
				jInfo.put("gname", gType.getName());
				array.add(jInfo);
			}
		}
		List subtype = subDao.query(name);
		for(int i = 0; i < subtype.size(); i++)
		{
			Subtype subType = (Subtype) subtype.get(i);
			Generaltype gType = gDao.findById(subType.getId().getGid());
			JSONObject jInfo = JSONObject.fromObject(subType.toString());
			jInfo.put("gname", gType.getName());
			array.add(jInfo);
		}
		if(array.size()==0){
			map.put("flag", "false");
			map.put("Info", "未添加任何数据");
		}
		else
		{
			map.put("flag", "true");
			map.put("Info", "获取成功");
			map.put("array", array.toString());
		}
		return JSONObject.fromObject(map).toString();
	}

}
